@echo off
chcp 65001 >nul
title 手持4G信号检测系统 - 高级调试工具

:MAIN_MENU
cls
echo ========================================
echo    手持4G信号检测系统 - 高级调试工具
echo ========================================
echo.
echo 请选择调试操作:
echo.
echo [1] 快速调试 (构建+安装+启动+日志)
echo [2] 仅构建应用
echo [3] 仅安装应用
echo [4] 仅启动应用
echo [5] 查看应用日志
echo [6] 清理日志缓存
echo [7] 查看设备信息
echo [8] 卸载应用
echo [9] 重启应用
echo [0] 退出
echo.
set /p choice=请输入选项 (0-9): 

if "%choice%"=="1" goto QUICK_DEBUG
if "%choice%"=="2" goto BUILD_ONLY
if "%choice%"=="3" goto INSTALL_ONLY
if "%choice%"=="4" goto START_ONLY
if "%choice%"=="5" goto VIEW_LOGS
if "%choice%"=="6" goto CLEAR_LOGS
if "%choice%"=="7" goto DEVICE_INFO
if "%choice%"=="8" goto UNINSTALL_APP
if "%choice%"=="9" goto RESTART_APP
if "%choice%"=="0" goto EXIT
goto INVALID_CHOICE

:QUICK_DEBUG
echo.
echo ========================================
echo           快速调试模式
echo ========================================
call :CHECK_ADB
call :BUILD_APP
call :INSTALL_APP
call :START_APP
call :VIEW_LOGS
goto MAIN_MENU

:BUILD_ONLY
echo.
echo ========================================
echo           构建应用
echo ========================================
call :CHECK_ADB
call :BUILD_APP
pause
goto MAIN_MENU

:INSTALL_ONLY
echo.
echo ========================================
echo           安装应用
echo ========================================
call :CHECK_ADB
call :INSTALL_APP
pause
goto MAIN_MENU

:START_ONLY
echo.
echo ========================================
echo           启动应用
echo ========================================
call :CHECK_ADB
call :START_APP
pause
goto MAIN_MENU

:VIEW_LOGS
echo.
echo ========================================
echo           查看应用日志
echo ========================================
call :CHECK_ADB
echo 按 Ctrl+C 停止日志监控
echo.
"%ADB_PATH%" logcat -s "handheld4_gsignal_app" -v time
pause
goto MAIN_MENU

:CLEAR_LOGS
echo.
echo ========================================
echo           清理日志缓存
echo ========================================
call :CHECK_ADB
"%ADB_PATH%" logcat -c
echo ✅ 日志缓存已清理
pause
goto MAIN_MENU

:DEVICE_INFO
echo.
echo ========================================
echo           设备信息
echo ========================================
call :CHECK_ADB
echo 设备列表:
"%ADB_PATH%" devices
echo.
echo 设备详细信息:
"%ADB_PATH%" shell getprop ro.product.model
"%ADB_PATH%" shell getprop ro.build.version.release
"%ADB_PATH%" shell getprop ro.build.version.sdk
pause
goto MAIN_MENU

:UNINSTALL_APP
echo.
echo ========================================
echo           卸载应用
echo ========================================
call :CHECK_ADB
echo 正在卸载应用...
"%ADB_PATH%" uninstall com.example.handheld4_gsignal_app
echo ✅ 应用已卸载
pause
goto MAIN_MENU

:RESTART_APP
echo.
echo ========================================
echo           重启应用
echo ========================================
call :CHECK_ADB
echo 正在停止应用...
"%ADB_PATH%" shell am force-stop com.example.handheld4_gsignal_app
timeout /t 2 /nobreak >nul
echo 正在启动应用...
call :START_APP
pause
goto MAIN_MENU

:INVALID_CHOICE
echo.
echo ❌ 无效选项，请重新选择
timeout /t 2 /nobreak >nul
goto MAIN_MENU

:EXIT
echo.
echo 感谢使用调试工具！
exit /b 0

:: ========================================
::           辅助函数
:: ========================================

:CHECK_ADB
set ADB_PATH=D:\Android\android-sdk\platform-tools\adb.exe
echo [检查] ADB连接状态...
"%ADB_PATH%" devices >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ 错误: ADB未找到或未正确配置
    echo 当前ADB路径: %ADB_PATH%
    pause
    exit /b 1
)

for /f "tokens=1" %%i in ('"%ADB_PATH%" devices ^| findstr "device$"') do set DEVICE_ID=%%i
if "%DEVICE_ID%"=="" (
    echo ❌ 错误: 未找到连接的Android设备
    pause
    exit /b 1
)
echo ✅ 设备已连接: %DEVICE_ID%
return

:BUILD_APP
echo [构建] 正在编译应用...
call gradlew assembleDebug
if %errorlevel% neq 0 (
    echo ❌ 构建失败
    pause
    exit /b 1
)
echo ✅ 应用构建成功
return

:INSTALL_APP
echo [安装] 正在安装应用...
call gradlew installDebug
if %errorlevel% neq 0 (
    echo ❌ 安装失败
    pause
    exit /b 1
)
echo ✅ 应用安装成功
return

:START_APP
echo [启动] 正在启动应用...
"%ADB_PATH%" shell am start -n com.example.handheld4_gsignal_app/.MainActivity
if %errorlevel% neq 0 (
    echo ❌ 应用启动失败
    pause
    exit /b 1
)
echo ✅ 应用启动成功
return